<meta charset="UTF-8">
<?php

require ('./lib/init.php');
// 判断用户是否登录
if (!acc()) {
    header('Location:login.php');
}
$art_id=$_GET['art_id'];
$cat_name=$_GET['cat_name'];
//判断地址栏传来的 art_id 是否合法
if (!is_numeric($art_id)){
    error ('文章id不合法！','javascript:history.back(-1)');
}
//判断catname 是否存在
$sql = "select * from cat where cat_name = '$cat_name'";
if (!mGetrow($sql)){
    error('该文章所处分类不存在','javascript:history.back(-1)');
}

//文章是否存在
$sql = "select * from art where art_id = $art_id";
if (!mGetrow($sql)){
    error('文章不存在','javascript:history.back(-1)');
}

//判断文章和分类是否一致
$sql = "select art_id,cat_name from art left join cat on cat.cat_id=art.cat_id where art_id = $art_id and cat_name = '$cat_name' ";
if (!mGetRow($sql)){
    error ('文章id和分类不一致','javascript:history.back(-1)');
}

//判断该文章是否有tag
$sql = "select * from tag where art_id = $art_id";
if (mGetRow($sql)){
//    存在则删除所有tag
    $sql = "delete from tag where art_id = $art_id";
    if (!mQuery($sql)){
        error ('服务器出错！','javascript:history.back(-1)');
    }
}

//删除文章
$sql = "delete from art where art_id = $art_id";
if (!mQuery($sql)){
    error ('文章删除失败','javascript:history.back(-1)');
}else{
    //succ ('文章删除成功');
//    对应cat下的文章num - 1
    $sql = "update cat set cat_num = cat_num - 1 where cat_name = '$cat_name' ";
    mQuery($sql);
    header('location:artlist.php');
}
?>
